package com.briup.bms.web.controller;

import com.briup.bms.bean.Employee;
import com.briup.bms.common.utils.Page;
import com.briup.bms.common.utils.Result;
import com.briup.bms.service.IEmpService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Api(tags = "员工信息服务")
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    IEmpService empService;

    @ApiOperation(value = "按条件分页显示员工信息", notes = "根据登陆的用户不同，显示的员工信息不同")
    @GetMapping("/by/condition")
    public Result<PageInfo<Employee>> findByCondition(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize,
            String username, Integer deptId,
            @DateTimeFormat(pattern = "yyyy-MM-dd")Date startDate,
            @DateTimeFormat(pattern = "yyyy-MM-dd")Date endDate, String title) {

        return Result.success(empService.findByConditon(
                pageNum, pageSize, username, deptId, startDate, endDate, title));
    }

    @GetMapping("/by/page")
    public Result<PageInfo<Employee>> findByPageHelper(
    		@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize){
    	return Result.success(empService.findByPageHelper(pageNum,pageSize));
	}
	@PostMapping("/saveOrUpdate")
	public Result<String> saveOrUpdateEmployee(@RequestBody Employee employee){
        empService.saveOrUpdateEmployee(employee);
        return Result.success(employee.getUsername());
    }
    @PutMapping("/update")
    public Result<String> updateEmployee(@RequestBody Employee employee){
        empService.updateEmployee(employee);
        return Result.success(employee.getUsername());
    }

    @ApiOperation(value = "统计各区域员工个数",notes = "统计出各个区域员工信息个数")
    @GetMapping("/statistic/by/region")
    public Result<Map<String, List<?>>> statisticEmpByRegion(){
        return Result.success(empService.statisticEmpByRegion());
    }

    @ApiOperation(value = "统计各区域员工个数",notes = "统计出各个区域员工信息个数")
    @GetMapping("/statistic/by/region2")
    public Result<List<Map<String, Object>>> statisticEmpByRegion2(){
        return Result.success(empService.statisticEmpByRegion2());
    }

    @DeleteMapping("/{empId}")
    public Result<String> deleteDepartment(@PathVariable("empId") String empId) {
        empService.deleteEmp(empId);
        return Result.success("删除"+empId+"成功");
    }
    @DeleteMapping("/batchDelete")
    public Result<String> batchDeleteEmp(@RequestParam("EmpIds") Set<Integer> EmpIds) {
        empService.batchDeleteEmp(EmpIds);
//        System.out.println(Arrays.toString(departmentIds));
        return Result.success("删除"+ EmpIds +"成功");
    }

}
